//
//  DSTabBarController.m
//  ZhiBo
//
//  Created by AntSailNet on 2017/11/7.
//  Copyright © 2017年 Befiv. All rights reserved.
//

#import "DSTabBarController.h"
#import "DSBaseViewController.h"
#import "DSNavigationController.h"
#import "PrefixHeader.pch"


@interface DSTabBarController (){
    
    //最近一次选择的Index
    
    NSUInteger _lastSelectedIndex;
    
}

@end

@implementation DSTabBarController
@synthesize lastSelectedIndex=_lastSelectedIndex;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildVC];
    self.selectedIndex = 2;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    //判断是否相等,不同才设置
    
    if (self.selectedIndex != selectedIndex) {
        //设置最近一次
        _lastSelectedIndex = self.selectedIndex;
       // NSLog(@"1 OLD:%lu , NEW:%d",(unsigned long)self.lastSelectedIndex,selectedIndex);
        
    }
    //调用父类的setSelectedIndex
    [super setSelectedIndex:selectedIndex];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    //获得选中的item
    NSUInteger tabIndex = [tabBar.items indexOfObject:item];
    
    if (tabIndex != self.selectedIndex) {
        //设置最近一次变更
        _lastSelectedIndex = self.selectedIndex;
        //NSLog(@"2 OLD:%lu , NEW:%lu",(unsigned long)self.lastSelectedIndex,(unsigned long)tabIndex);
        
    }
    
}


#pragma mark - 添加自控制器
- (void)addChildVC
{
//    [self addChildVC:[[MaterialsMallViewController alloc]init] title:@"建材商城" image:@"tab_goodsunselect" selectedImage:@"tab_goodsselect"];
//    [self addChildVC:[[DecorateMethodViewController alloc] init] title:@"攻略" image:@"tab_glunselect" selectedImage:@"tab_glselect"];
//    [self addChildVC:[[RootViewController alloc] init] title:@"置家首页" image:@"tab_homeunselect" selectedImage:@"tab_homeselect"];
//    [self addChildVC:[[ShoppingCartViewController alloc]init] title:@"购物车" image:@"tab_scunselect" selectedImage:@"tab_scselect"];
//    [self addChildVC:[[PersonalCenterViewController alloc] init] title:@"我的" image:@"tab_perunselect" selectedImage:@"tab_perselect"];
}

#pragma mark - 子控制器样式
- (void)addChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName
{
    childVC.title = title;
    
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置文字样式
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = GRAY_COLOR;
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = BODY_COLOR;
    [childVC.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    //设置导航控制器为子控制器
    DSNavigationController *nav = [[DSNavigationController alloc] initWithRootViewController:childVC];
//    if ([childVC isKindOfClass:[LiveVideoViewController class]]) {
//        [self addChildViewController:childVC];
//    }else{
    [self addChildViewController:nav];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
